<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/Language.php';
require_once 'CMS/User.php';

//Người tạo : Nguyễn Hồng Danh - Team 2
//Module 	: DescriptionNews
//Chức năng	: Show ra mô tả của tin tức
 
class ProductDetail_ClickChangeController extends BaseUserController{

	public function init(){
		parent::init();
	}

	public function indexAction()
	{
		$this->_helper->layout->disableLayout();
		
		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/productdetail');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
		}
		else
			$language->load('Default');		
		/*========== End Load Language ==========*/
		
		//-------------------- get all languages ---------------------//
		$languages = $tbLanguage->getAllLanguages();
		$this->view->languages = $languages;	
		
		/*========== Start Get Text ==============*/
		$this->view->title		= $language->get('text_title');
		$this->view->textProductName		= $language->get('text_ProductName');
		$this->view->textCate 		= $language->get('text_cate');
		$this->view->textImage		= $language->get('text_Image');
		$this->view->textChangeImage 		= $language->get('text_ChangeImage');
		$this->view->RelateImage 		= $language->get('text_RelateImage');
		$this->view->ButtonName		= $language->get('text_ButtonName');
		$this->view->Price		= $language->get('text_Price');
		$this->view->Productinfo		= $language->get('text_Productinfo');
		$this->view->ShortDes		= $language->get('text_ShortDes');
		$this->view->Save	= $language->get('text_Save');
		$this->view->textError1 	= $language->get('error1');
		$this->view->textError2 	= $language->get('error2');
		$this->view->textError3 	= $language->get('error3');
		$this->view->textError4 	= $language->get('error4');
		$this->view->textError5 	= $language->get('error5');
		$this->view->textError6 	= $language->get('error6');
		$this->view->textError7 	= $language->get('error7');
		$this->view->textError8 	= $language->get('error8');
		$this->view->textError9 	= $language->get('error9');
		$this->view->textError100 	= $language->get('error100');
		/*========== End Text ==============*/
		
		/*========== Start Check Permission ==========*/
		$path = APPLICATION_PATH."/widgets/productdetail/";
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);
		
		$user = new User();
		$user->init();
		$user->check_login();
		$hasmodify = $user->modifiy_content($xmlconfig->name);
		if($hasmodify=="false")
		{
			$this->view->error = 100 ;
			return;
		}
		/*========== End Check Permission ==========*/
		
		//-----------------------Khai báo các parameter----------------------
		$tbThings= new ProductDetail_Model_Things();
		$tbImage= new ProductDetail_Model_Imagetothing();	
		$tbCate=new ProductDetail_Model_Categories();
		$dateInt = new Util();
		$access=false;
		//---------------------- Khai báo các parameter----------------------//
		
		//-----------------------Check Input Parameter-----------------------
		if(isset($_GET["id"]) && $dateInt->is_interger($_GET["id"]))
		{
			//Check thing_id có trong db?
			$temp = $tbThings->getThingById($_GET["id"]);
			$ProductDetail = $temp[0];
			$desc= unserialize($ProductDetail['short_description']);
			$common_info = unserialize($ProductDetail['common_info']);
			
			//print_r(htmlentities($ProductDetail['common_info']));
			//die;
			
			if(count($ProductDetail)>0)
				$access=true;
		}
		if(!$access) 
		{
			$this->_redirect('/index/index');
		}
		if(isset($_GET["action"])&&($_GET["action"]=="edit")&&(!isset($this->view->error)))
		{
			if(!isset($_POST["product_name"])||(strlen($_POST["product_name"])<10))
			{
				$this->view->error = 2;
				$this->view->error_title = 1;
			}
			if(!isset($_POST["product_category"])||(!is_numeric($_POST["product_category"]))||($_POST["product_category"]<=0))
			{
				$this->view->error = 3;
				$this->view->error_cate = 1;
			}
			if(!isset($_POST["product_price"])||(!is_numeric($_POST["product_price"]))||($_POST["product_price"]<=1000))
			{
				$this->view->error = 4;
				$this->view->error_price = 1;
			}
			$error_desc = array();
			
			//check default
			
			if(!isset($_POST["product_description"]['Default'])||(strlen($_POST["product_description"]['Default'])<20))
			{
				
				$this->view->error = 5;
				$error_desc['Default'][0] = $language->get('error9');
			}
			if(!isset($_POST["product_short_description"]['Default'])||(strlen($_POST["product_short_description"]['Default'])<10))
			{
			
				$this->view->error = 6;
				$error_desc['Default'][1] = $language->get('error8');
			}
			//check multi language			
			foreach($languages as $lang){
				if(!isset($_POST["product_description"][$lang['name']])||(strlen($_POST["product_description"][$lang['name']])<20))
				{
				
					$this->view->error = 5;
					$error_desc[$lang['name']][0] = $language->get('error9');
				}
				if(!isset($_POST["product_short_description"][$lang['name']])||(strlen($_POST["product_short_description"][$lang['name']])<10))
				{
					$this->view->error = 6;
					$error_desc[$lang['name']][1] = $language->get('error8');
				}
			}
			$this->view->error_desc = $error_desc;			

		}
		//-----------------------Check Input Parameter------------------------//
		if(isset($_GET["action"])&&($_GET["action"]=="edit")&&(!isset($this->view->error)))
		{
			foreach($_POST["product_description"] as $lang1 => $value)
			{
				$_POST["product_description"][$lang1]=str_replace(array('\\'),array(""),$value);
				//$_POST["product_description"][$lang1]=str_replace(array('<p>','</p>'),array(" "),$value);
				//print_r($_POST["product_description"][$lang1]);die;//cho nai moi replace ma bo'
			}
			
			$editData = array(
								'title'			=>	$_POST["product_name"],
								'category_id'	=>	$_POST["product_category"],
								'price' 		=>	$_POST["product_price"],
								'common_info'	=>	serialize($_POST["product_description"]),
								'short_description'	=>	serialize($_POST["product_short_description"]),
								'avatar'		=>	$_POST["product_avatar"],
								'type'			=>	1,
								'update_date'	=>	$dateInt->get_time_now(),
								'deleted'		=>	0
								);
			
			//update cho nao`????
			
			$result = $tbThings->updateThing($_GET['id'], $editData);
			$this->view->error = $result;

			$thing_id = $_GET['id'];
			if($tbImage->deletething($thing_id) == 1){
				foreach ($_POST["relatedImageUpload"] as $image)
				{
					if($image!="" && $image!=null)
					{
						$inserData = array(
											'thing_id' => $thing_id,
											'url' => $image
						);
						$result = $tbImage->insertImage($inserData);
					}
				}
			}			
			
		}
			
				
		//Lấy tất cả image của sp
		$AllImage=$tbImage->getImageByThingsId($_GET["id"]);
		
		//Lấy ngày đăng tin
		$Update_Date=$dateInt->makeDays($ProductDetail['update_date']);
		
		//Lấy các danh mục sp con
		$getAllLeafCate=$tbCate->getLeafCategoriesbyType(1);
		$CategoriesProduct = array();
		if(isset($name['name']) && $name['name'] != '')
			$nL = $name['name'];
		else 
			$nL = 'Default';			
		foreach ($getAllLeafCate as $c_t){
			$t_c = unserialize($c_t['category_name']);
			if(!isset($t_c[$nL])){
				$nameCate = $t_c['Default'];
			}else{
				$nameCate = $t_c[$nL];
			}
			$CategoriesProduct[] = array(
											'category_id'	=> $c_t['category_id'],
											'category_name' => $nameCate
										);
		}		
		//---------------------Truyền parameter qua View-----------------------
		$this->view->Update_Date=$Update_Date;				
		$this->view->ProductDetail =$ProductDetail;
		$this->view->description = $common_info;
		$this->view->short_des = $desc; 	
		$this->view->AllImage=$AllImage;	
		$this->view->getAllLeafCate=$CategoriesProduct;
		//---------------------Truyền parameter qua View-----------------------//
		
		if(isset($_POST["product_name"]))
			$this->view->new_product_name=$_POST["product_name"];
		if(isset($_POST["product_price"]))
			$this->view->new_product_price=$_POST["product_price"];
		if(isset($_POST["product_category"]))
			$this->view->new_product_category=$_POST["product_category"];
		if(isset($_POST["product_description"]))
			$this->view->description=$_POST["product_description"];
		if(isset($_POST["product_short_description"]))
			$this->view->short_des=$_POST["product_short_description"];
		if(isset($_POST["product_avatar"]))
			$this->view->new_product_avatar=$_POST["product_avatar"];
		if(isset($_POST["relatedImageUpload"]))
		{
			$temp_no=0;
			if(count($_POST["relatedImageUpload"])>0)
				foreach ($_POST["relatedImageUpload"] as $image)
				{
					if($image!="" && $image!=null)
					{
						$this->view->AllImage[$temp_no]=array('url' => $image);
						$temp_no++;
					}
				}
		}

		
	}
}